<!DOCTYPE HTML>
<html lang="ch" class="sidebar-visible no-js light">
    <head>
        <!-- Book generated using mdBook -->
        <meta charset="UTF-8">
        <title>准备：实验环境搭建 - nudt os lab book (2023)</title>


        <!-- Custom HTML head -->
        
        <meta name="description" content="">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <meta name="theme-color" content="#ffffff" />

        <link rel="icon" href="favicon.svg">
        <link rel="shortcut icon" href="favicon.png">
        <link rel="stylesheet" href="css/variables.css">
        <link rel="stylesheet" href="css/general.css">
        <link rel="stylesheet" href="css/chrome.css">
        <link rel="stylesheet" href="css/print.css" media="print">

        <!-- Fonts -->
        <link rel="stylesheet" href="FontAwesome/css/font-awesome.css">
        <link rel="stylesheet" href="fonts/fonts.css">

        <!-- Highlight.js Stylesheets -->
        <link rel="stylesheet" href="highlight.css">
        <link rel="stylesheet" href="tomorrow-night.css">
        <link rel="stylesheet" href="ayu-highlight.css">

        <!-- Custom theme stylesheets -->

    </head>
    <body>
    <div id="body-container">
        <!-- Provide site root to javascript -->
        <script>
            var path_to_root = "";
            var default_theme = window.matchMedia("(prefers-color-scheme: dark)").matches ? "navy" : "light";
        </script>

        <!-- Work around some values being stored in localStorage wrapped in quotes -->
        <script>
            try {
                var theme = localStorage.getItem('mdbook-theme');
                var sidebar = localStorage.getItem('mdbook-sidebar');

                if (theme.startsWith('"') && theme.endsWith('"')) {
                    localStorage.setItem('mdbook-theme', theme.slice(1, theme.length - 1));
                }

                if (sidebar.startsWith('"') && sidebar.endsWith('"')) {
                    localStorage.setItem('mdbook-sidebar', sidebar.slice(1, sidebar.length - 1));
                }
            } catch (e) { }
        </script>

        <!-- Set the theme before any content is loaded, prevents flash -->
        <script>
            var theme;
            try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { }
            if (theme === null || theme === undefined) { theme = default_theme; }
            var html = document.querySelector('html');
            html.classList.remove('no-js')
            html.classList.remove('light')
            html.classList.add(theme);
            html.classList.add('js');
        </script>

        <!-- Hide / unhide sidebar before it is displayed -->
        <script>
            var html = document.querySelector('html');
            var sidebar = null;
            if (document.body.clientWidth >= 1080) {
                try { sidebar = localStorage.getItem('mdbook-sidebar'); } catch(e) { }
                sidebar = sidebar || 'visible';
            } else {
                sidebar = 'hidden';
            }
            html.classList.remove('sidebar-visible');
            html.classList.add("sidebar-" + sidebar);
        </script>

        <nav id="sidebar" class="sidebar" aria-label="Table of contents">
            <div class="sidebar-scrollbox">
                <ol class="chapter"><li class="chapter-item expanded affix "><li class="part-title">操作系统实验指南</li><li class="chapter-item expanded "><a href="lab_0.html" class="active"><strong aria-hidden="true">1.</strong> 准备：实验环境搭建</a></li><li class="chapter-item expanded "><a href="lab_1.html"><strong aria-hidden="true">2.</strong> 实验1：向Framebuffer中打印字符</a></li><li class="chapter-item expanded "><a href="lab_2.html"><strong aria-hidden="true">3.</strong> 实验2：异常处理和硬件中断</a></li><li class="chapter-item expanded "><a href="lab_3.html"><strong aria-hidden="true">4.</strong> 实验3：内存管理</a></li><li class="chapter-item expanded "><a href="lab_4.html"><strong aria-hidden="true">5.</strong> 实验4：协程与异步</a></li></ol>
            </div>
            <div id="sidebar-resize-handle" class="sidebar-resize-handle"></div>
        </nav>

        <div id="page-wrapper" class="page-wrapper">

            <div class="page">
                                <div id="menu-bar-hover-placeholder"></div>
                <div id="menu-bar" class="menu-bar sticky bordered">
                    <div class="left-buttons">
                        <button id="sidebar-toggle" class="icon-button" type="button" title="Toggle Table of Contents" aria-label="Toggle Table of Contents" aria-controls="sidebar">
                            <i class="fa fa-bars"></i>
                        </button>
                        <button id="theme-toggle" class="icon-button" type="button" title="Change theme" aria-label="Change theme" aria-haspopup="true" aria-expanded="false" aria-controls="theme-list">
                            <i class="fa fa-paint-brush"></i>
                        </button>
                        <ul id="theme-list" class="theme-popup" aria-label="Themes" role="menu">
                            <li role="none"><button role="menuitem" class="theme" id="light">Light</button></li>
                            <li role="none"><button role="menuitem" class="theme" id="rust">Rust</button></li>
                            <li role="none"><button role="menuitem" class="theme" id="coal">Coal</button></li>
                            <li role="none"><button role="menuitem" class="theme" id="navy">Navy</button></li>
                            <li role="none"><button role="menuitem" class="theme" id="ayu">Ayu</button></li>
                        </ul>
                        <button id="search-toggle" class="icon-button" type="button" title="Search. (Shortkey: s)" aria-label="Toggle Searchbar" aria-expanded="false" aria-keyshortcuts="S" aria-controls="searchbar">
                            <i class="fa fa-search"></i>
                        </button>
                    </div>

                    <h1 class="menu-title">nudt os lab book (2023)</h1>

                    <div class="right-buttons">
                        <a href="print.html" title="Print this book" aria-label="Print this book">
                            <i id="print-button" class="fa fa-print"></i>
                        </a>

                    </div>
                </div>

                <div id="search-wrapper" class="hidden">
                    <form id="searchbar-outer" class="searchbar-outer">
                        <input type="search" id="searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="searchresults-outer" aria-describedby="searchresults-header">
                    </form>
                    <div id="searchresults-outer" class="searchresults-outer hidden">
                        <div id="searchresults-header" class="searchresults-header"></div>
                        <ul id="searchresults">
                        </ul>
                    </div>
                </div>

                <!-- Apply ARIA attributes after the sidebar and the sidebar toggle button are added to the DOM -->
                <script>
                    document.getElementById('sidebar-toggle').setAttribute('aria-expanded', sidebar === 'visible');
                    document.getElementById('sidebar').setAttribute('aria-hidden', sidebar !== 'visible');
                    Array.from(document.querySelectorAll('#sidebar a')).forEach(function(link) {
                        link.setAttribute('tabIndex', sidebar === 'visible' ? 0 : -1);
                    });
                </script>

                <div id="content" class="content">
                    <main>
                        <h1 id="准备实验环境搭建"><a class="header" href="#准备实验环境搭建">准备：实验环境搭建</a></h1>
<h2 id="实验步骤"><a class="header" href="#实验步骤">实验步骤</a></h2>
<p>在连接互联网的情况下，可按如下步骤实施。</p>
<ol>
<li>
<p>安装Ubuntu2204虚拟机</p>
<ul>
<li><a href="https://ubuntu.com/download/desktop">Ubuntu2204下载</a></li>
<li><a href="https://www.vmware.com/in/products/workstation-player/workstation-player-evaluation.html">VmWare下载</a></li>
<li>创建新虚拟机，导入下载的Ubuntu2204镜像，按操作指引完成系统安装（磁盘容量大于50G）</li>
</ul>
</li>
<li>
<p>在虚拟机中安装Rust开发工具链</p>
<ul>
<li>安装依赖软件：
<ul>
<li><code>sudo apt install make gcc qemu-system-x86 curl</code></li>
</ul>
</li>
<li>按<a href="https://www.rust-lang.org/tools/install">官方说明</a>安装Rust</li>
<li>按以下命令安装工具
<ul>
<li><code>rustup target add x86_64-unknown-none</code></li>
<li><code>rustup component add llvm-tools-preview</code></li>
<li><code>rustup component add rust-src</code></li>
</ul>
</li>
</ul>
</li>
<li>
<p><a href="https://visualstudio.microsoft.com/zh-hans/">Visual Studio下载</a>及Rust工具安装</p>
<ul>
<li>打开VS，选择<code>扩展</code>按钮，搜索<code>rust-analyzer</code>并安装该插件</li>
<li>有关Rust的语法，可以阅读参考资料1.</li>
</ul>
</li>
<li>
<p>运行实验示例代码</p>
<ul>
<li>在VS中打开示例代码<code>rust_os-lab1</code>文件夹，并开启终端</li>
<li>在终端输入<code>make run</code>运行成功后应看到弹出的QEMU界面，如下所示：
<img src="pic/lab0-sc.png" alt="图片" /></li>
</ul>
</li>
</ol>
<p>至此，实验初始环境搭建完成。</p>
<p>大家可以阅读<code>README</code>文件，尝试运行更多的命令，并了解各命令的功能。</p>
<h2 id="实验示例代码说明"><a class="header" href="#实验示例代码说明">实验示例代码说明</a></h2>
<p>示例代码是一个最小可运行的系统，包括了boot和kernel两个主要模块，分别为加载器和内核。</p>
<p>由于内核本身无法独立运行，加载器的主要作用是将内核可执行程序打包为一个BIOS或UEFI规范可启动的镜像，系统加电后先执行加载器，之后加载器拉器内核。boot模块中的另一个作用是执行qemu命令，从而支持在模拟器中开发调试内核。目前，我们暂时先不关心加载器的运行原理，过多的细节容易入门即放弃。</p>
<p>我们的系统虽小，但已经有很多非常棒的功能辅助我们开发调试：</p>
<ul>
<li>
<p>串口输出。在主函数<code>kernel_main</code>中我们使用串口打印了<code>Hello world</code>，通过<code>make run</code>命令运行系统后，我们可以在终端看到输出。</p>
</li>
<li>
<p>GDB调试。代码中提供了一些小脚本可以方便的使用GDB进行调试，只需在一个终端运行<code>make gdb</code>，另一个终端运行<code>gdb -n -x .gdbinit</code>。GDB是Debug神器，如果不清楚如何使用，快查手册。</p>
</li>
<li>
<p>测试框架。代码中包括了单元测试和集成测试框架，kernel目录中包括了lib和main两个主文件，Rust会<a href="https://doc.rust-lang.org/cargo/guide/project-layout.html">分别</a>生成<code>libkernel库</code>和<code>kernel可执行程序</code>。lib实现了内核的主体逻辑，其中包含的#[test_case]函数为单元测试用例。main是内核的入口，到用了lib提供的功能。集成测试的用例类似于main，有独立的入口，复用lib提供的功能（如kernel/tests/chr2pixels.rs，可以有多个）。通过<code>make test</code>命令可以依次执行单元测试和各个集成测试。</p>
</li>
<li>
<p>注释文档。Rust支持<a href="https://doc.rust-lang.org/rustdoc/index.html">注释文档</a>机制，即通过特定的格式书写注释，便可自动生成文档。我们在各个主文件中标记了<code>#![warn(missing_docs)]</code>和<code>#![deny(warnings)]</code>，分别意为<code>缺少注释文档时warnings</code>和<code>拒绝一切warnings</code>，联合起来就是<code>不能缺少注释文档</code>，强迫大家写好注释，功在当代利在千秋。通过<code>make doc</code>命令可以查看自己写的注释文档。</p>
</li>
</ul>
<h2 id="参考资料"><a class="header" href="#参考资料">参考资料</a></h2>
<ol>
<li>Rust语言学习网站
<ul>
<li><a href="https://www.runoob.com/rust/rust-tutorial.html">https://www.runoob.com/rust/rust-tutorial.html</a></li>
<li><a href="https://course.rs/about-book.html">https://course.rs/about-book.html</a></li>
</ul>
</li>
<li>GDB调试工具使用: <a href="https://zhuanlan.zhihu.com/p/74897601">https://zhuanlan.zhihu.com/p/74897601</a></li>
<li>Rust语言实现操作系统博客
<ul>
<li>原版：<a href="https://os.phil-opp.com/">https://os.phil-opp.com/</a></li>
<li>中文翻译版：<a href="https://www.bookstack.cn/books/writing-an-os-in-rust">https://www.bookstack.cn/books/writing-an-os-in-rust</a></li>
</ul>
</li>
</ol>

                    </main>

                    <nav class="nav-wrapper" aria-label="Page navigation">
                        <!-- Mobile navigation buttons -->

                            <a rel="next" href="lab_1.html" class="mobile-nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
                                <i class="fa fa-angle-right"></i>
                            </a>

                        <div style="clear: both"></div>
                    </nav>
                </div>
            </div>

            <nav class="nav-wide-wrapper" aria-label="Page navigation">

                    <a rel="next" href="lab_1.html" class="nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
                        <i class="fa fa-angle-right"></i>
                    </a>
            </nav>

        </div>

        <!-- Livereload script (if served using the cli tool) -->
        <script>
            const wsProtocol = location.protocol === 'https:' ? 'wss:' : 'ws:';
            const wsAddress = wsProtocol + "//" + location.host + "/" + "__livereload";
            const socket = new WebSocket(wsAddress);
            socket.onmessage = function (event) {
                if (event.data === "reload") {
                    socket.close();
                    location.reload();
                }
            };

            window.onbeforeunload = function() {
                socket.close();
            }
        </script>



        <script>
            window.playground_copyable = true;
        </script>


        <script src="elasticlunr.min.js"></script>
        <script src="mark.min.js"></script>
        <script src="searcher.js"></script>

        <script src="clipboard.min.js"></script>
        <script src="highlight.js"></script>
        <script src="book.js"></script>

        <!-- Custom JS scripts -->


    </div>
    </body>
</html>
